package io.github.uicdb.liba.api.util;

import io.github.uicdb.liba.api.blocks.data.BlockFacingFor4Side;
import io.github.uicdb.liba.api.register.BlockHolder;
import io.github.uicdb.liba.api.register.ItemHolder;
import net.minecraft.core.Direction;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
import net.minecraftforge.client.model.generators.BlockStateProvider;
import net.minecraftforge.client.model.generators.ConfiguredModel;
import net.minecraftforge.client.model.generators.ItemModelProvider;
import net.minecraftforge.client.model.generators.ModelFile;

import java.util.function.BiConsumer;

public class ModelUtil {
    public static BiConsumer<BlockStateProvider, BlockHolder<?>> plantModel(){
        return (provider, blockHolder) ->
                provider.simpleBlock(blockHolder.get(), provider.models().cross(blockHolder.get().getRegistryName().toString(),new ResourceLocation( blockHolder.get().getRegistryName().getNamespace(),"block/"+blockHolder.get().getRegistryName().getPath())));
    }
    public static BiConsumer<ItemModelProvider, ItemHolder<?>> useBlockTexture() {
        return  (provider, itemHolder) -> {
            var item = itemHolder.get().getRegistryName();
            provider.getBuilder(item.toString())
                    .parent(new ModelFile.UncheckedModelFile("item/generated"))
                    .texture("layer0", new ResourceLocation(item.getNamespace(), "block/" + item.getPath()));
        };
    }
    public static BiConsumer<BlockStateProvider, BlockHolder<?>> useModel(){
        return (provider, blockHolder) -> {
            var res = blockHolder.get().getRegistryName();
            var model= new ModelFile.UncheckedModelFile(new ResourceLocation(res.getNamespace(), "block/" + res.getPath()));
            if(blockHolder.get() instanceof BlockFacingFor4Side blockFacingFor4Side){
                provider.getVariantBuilder(blockFacingFor4Side).forAllStates(state -> {
                    var ret = new ConfiguredModel(model, 0, getRotY(state.getValue(BlockStateProperties.HORIZONTAL_FACING)), true);
                    return new ConfiguredModel[]{ret};
                });
            }else
                provider.simpleBlock(blockHolder.get(),model);
        };
    }
    /**
     * 根据方块方向提供旋转角度
     */
    public static int getRotY(Direction direction) {
        return switch (direction) {
            case EAST -> 90;
            case SOUTH -> 180;
            case WEST -> 270;
            default -> 0;
        };
    }
}
